package com.greenleaf.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.greenleaf.entity.PdtDetail;
import com.greenleaf.entity.PdtSize;
import com.greenleaf.entity.SearchCon;
import com.greenleaf.service.PdtDetailService;
import com.greenleaf.service.PdtSizeService;
@Controller
@RequestMapping("/pdtSize")
public class PdtSizeController {
	@Resource
	private PdtSizeService pdtSizeService;
	@Resource
	private PdtDetailService pdtDetailService;
	@RequestMapping("/getstock")
	@ResponseBody
	//http://localhost:8080/Greenleaf/pdtSize/getstock
	public Object getStock(SearchCon searchCon) {
		List<PdtSize> user = pdtSizeService.getAllPdtSize(searchCon);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count",pdtSizeService.getTotalPdtSize(searchCon));
		map.put("data", user);
		return map;
	}
	@RequestMapping("/get")
	@ResponseBody
	//http://localhost:8080/Greenleaf/pdtSize/get
	public Object getPdtSize(String ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<Integer> list = new ArrayList<Integer>();
		for (String id : ids.split(",")) {
			list.add(Integer.parseInt(id));
		}
		List<PdtDetail> pdtDetail = pdtDetailService.getPdtDetailByIds(list);
		for (PdtDetail pdt : pdtDetail) {
			PdtSize pdtSize= new PdtSize(null, pdt.getSize(), pdt.getPdtId(), null);
			int stock = pdtSizeService.getPdtSize(pdtSize);
			if(stock<pdt.getNumber()) {
				map.put("msg", false);
				map.put("data", "您要购买的商品:"+pdt.getPdtName()+"的规格："+pdt.getSize()+"库存不足");
				return map;
			}
		}
		map.put("msg", true);
		map.put("data", "");
		return map;
	}
	
	@RequestMapping("/update")
	@ResponseBody
	//http://localhost:8080/Greenleaf/pdtSize/update
	public Object updateProduct(PdtSize pdtSize) {
		//System.out.println(product);
		int r = pdtSizeService.updatePdtSize(pdtSize);
		return r!=0?true:false;
	}
}
